package com.ytktt.study.week9.task3.rpcfx;

import com.alibaba.fastjson.parser.ParserConfig;
import com.ytktt.study.week9.task3.rpcfx.constant.RpcfxConstant;
import com.ytktt.study.week9.task3.rpcfx.server.RpcfxServer;
import com.ytktt.study.week9.task3.rpcfx.server.RpcfxServiceProviderAutoConfigureRegistrar;
import com.ytktt.study.week9.task3.rpcfx.service.g1.OrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
 * 消费者应用启动器
 *
 * @author ytk
 */
@Slf4j
@Import({RpcfxServiceProviderAutoConfigureRegistrar.class})
@SpringBootApplication
public class RpcfxServerApplication implements ApplicationRunner {

    @Autowired
    private RpcfxServer rpcfxServer;

    public static void main(String[] args) throws Exception {


		// register service
		// xxx "com.ytktt.study.week9.task3.rpcfx.api.UserService"
//		String orderService = "io.kimking.rpcfx.demo.api.OrderService";
//		registerService(client, orderService);


        // 进一步的优化，是在spring加载完成后，从里面拿到特定注解的bean，自动注册到zk

        SpringApplication.run(RpcfxServerApplication.class, args);
    }

    @Bean(name = "com.ytktt.study.week9.task3.rpcfx.api.OrderService")
    public OrderService createOrderService(){
        return new OrderServiceImpl();
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        ParserConfig.getGlobalInstance().addAccept(RpcfxConstant.SERVICE_PACKAGE_PREFIX + '.' + RpcfxConstant.API_PACKAGE_PREFIX + '.');

        this.rpcfxServer.run();
    }
}
